/*
 * Copyright (c) 2020 pig4cloud Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.love.cloud.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.love.cloud.admin.api.dto.UserDTO;
import com.love.cloud.admin.api.entity.SysUser;
import com.love.cloud.admin.api.vo.*;
import com.love.cloud.admin.service.SysUserService;
import com.love.cloud.common.core.util.R;
import com.love.cloud.common.log.annotation.SysLog;
import com.love.cloud.common.security.annotation.AnonymousAccess;
import com.love.cloud.common.security.annotation.Inner;
import com.love.cloud.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author lengleng
 * @date 2019/2/1
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
@Api(value = "user", tags = "用户管理模块")
public class UserController {

	private final SysUserService userService;

	/**
	 * 获取当前用户全部信息
	 * @return 用户信息
	 */
	@GetMapping(value = { "/info" })
	public R info() {
		Long id = SecurityUtils.getUser().getId();
		SysUser user = userService.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getId, id));
		if (user == null) {
			return R.failed("获取当前用户信息失败");
		}
		return R.ok(userService.getUserInfo(user));
	}

	/**
	 * 获取指定用户全部信息
	 * @return 用户信息
	 */
	@Inner
	@GetMapping("/info/{username}")
	public R info(@PathVariable String username) {
		SysUser user = userService.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getUsername, username));
		if (user == null) {
			return R.failed(String.format("用户信息为空 %s", username));
		}
		return R.ok(userService.getUserInfo(user));
	}

	/**
	 * 通过ID查询用户信息
	 * @param id ID
	 * @return 用户信息
	 */
	@GetMapping("/{id}")
	public R user(@PathVariable Long id) {
		return R.ok(userService.getUserVoById(id));
	}

	/**
	 * 根据用户名查询用户信息
	 * @param username 用户名
	 * @return
	 */
	@GetMapping("/details/{username}")
	public R user(@PathVariable String username) {
		SysUser condition = new SysUser();
		condition.setUsername(username);
		return R.ok(userService.getOne(new QueryWrapper<>(condition)));
	}

	/**
	 * 删除用户信息
	 * @param id ID
	 * @return R
	 */
	@SysLog("删除用户信息")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('sys_user_del')")
	public R userDel(@PathVariable Long id) {
		SysUser sysUser = userService.getById(id);
		return R.ok(userService.removeUserById(sysUser));
	}

	/**
	 * 添加用户
	 * @param userDto 用户信息
	 * @return success/false
	 */
	@SysLog("添加用户")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('sys_user_add')")
	public R user(@RequestBody UserDTO userDto) {
		return R.ok(userService.saveUser(userDto));
	}

	/**
	 * 更新用户信息
	 * @param userDto 用户信息
	 * @return R
	 */
	@SysLog("更新用户信息")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('sys_user_edit')")
	public R updateUser(@Valid @RequestBody UserDTO userDto) {
		return R.ok(userService.updateUser(userDto));
	}

	/**
	 * 分页查询用户
	 * @param page 参数集
	 * @param userDTO 查询参数列表
	 * @return 用户集合
	 */
	@GetMapping("/page")
	public R getUserPage(Page page, UserDTO userDTO) {
		return R.ok(userService.getUserWithRolePage(page, userDTO));
	}

	/**
	 * 修改个人信息
	 * @param userDto userDto
	 * @return success/false
	 */
	@SysLog("修改个人信息")
	@PutMapping("/edit")
	public R updateUserInfo(@Valid @RequestBody UserDTO userDto) {
		return R.ok(userService.updateUserInfo(userDto));
	}

	/**
	 * 修改密码
	 * @param userDto userDto
	 * @return success/false
	 */
	@SysLog("修改密码")
	@PutMapping("/changePassword")
	public R changePassword(@Valid @RequestBody UserDTO userDto) {
		return R.ok(userService.changePassword(userDto));
	}
	/**
	 * 重置密码
	 * @param userDto userDto
	 * @return success/false
	 */
	@SysLog("重置密码")
	@PutMapping("/resetPassword")
	public R resetPassword(@Valid @RequestBody UserDTO userDto) {
		return R.ok(userService.resetPassword(userDto));
	}


	/**
	 * 忘记密码-获取验证码
	 * @param forgetPasswordVo forgetPasswordVo
	 * @return
	 */
	@SysLog("忘记密码-获取验证码")
	@PostMapping("/getForgetPasswordValidCode")
	@ApiOperation(value = "忘记密码-获取验证码")
	@AnonymousAccess
	public Object getForgetPasswordValidCode(@Validated(ForgetPasswordVo.GetValidCode.class) @RequestBody ForgetPasswordVo forgetPasswordVo) {
		return userService.getForgetPasswordValidCode(forgetPasswordVo);
	}

	/**
	 * 忘记密码-校验验证码
	 * @param forgetPasswordVo forgetPasswordVo
	 * @return
	 */
	@SysLog("忘记密码-校验验证码")
	@PostMapping("/checkForgetPasswordValidCode")
	@ApiOperation(value = "忘记密码-校验验证码")
	@AnonymousAccess
	public Object checkForgetPasswordValidCode(@Validated(ForgetPasswordVo.CheckValidCode.class) @RequestBody ForgetPasswordVo forgetPasswordVo) {
		return userService.checkForgetPasswordValidCode(forgetPasswordVo);
	}


	/**
	 * 忘记密码-设置新的密码
	 * @param forgetPasswordVo forgetPasswordVo
	 * @return
	 */
	@SysLog("忘记密码-设置新的密码")
	@PutMapping("/forgetPasswordSetNewPassword")
	@ApiOperation(value = "忘记密码-设置新的密码")
	@AnonymousAccess
	public Object forgetPasswordSetNewPassword(@Validated(ForgetPasswordVo.SetNewPassword.class) @RequestBody ForgetPasswordVo forgetPasswordVo) {
		return userService.forgetPasswordSetNewPassword(forgetPasswordVo);
	}


	/**
	 * 获取验证码
	 * @param forgetPasswordVo forgetPasswordVo
	 * @return
	 */
	@SysLog("获取验证码")
	@PostMapping("/getValidCode")
	@ApiOperation(value = "绑定手机号-获取验证码")
	public Object getValidCode(@Validated(ForgetPasswordVo.GetValidCode.class) @RequestBody ForgetPasswordVo forgetPasswordVo) {
		return userService.getValidCode(forgetPasswordVo);
	}

	/**
	 * 绑定手机号
	 * @param
	 * @return
	 */
	@SysLog("绑定手机号")
	@PutMapping("/bindMobile")
	public R bindMobile(@Validated(ForgetPasswordVo.CheckValidCode.class)  @RequestBody ForgetPasswordVo forgetPasswordVo) {
		return R.ok(userService.bindMobile(forgetPasswordVo));
	}

	/**
	 * 获取到手机号
	 * @param
	 * @return
	 */
	@SysLog("获取到手机号")
	@GetMapping("/getMobile")
	public R getMobile() {
		return R.ok(userService.getMobile());
	}

	@PutMapping("/updateUserStatus")
	public Object updateUserStatus(@Validated(UpdateUserVo.UpdateUserStatus.class) @RequestBody UpdateUserVo updateUserVo){
		return userService.updateUserStatus(updateUserVo);
	}

	@PostMapping("/getDeptUsers")
	@ApiOperation(value = "获取部门用户树列表")
	public Object getDeptUsers(DeptUsersVo deptUsersVo){
		return userService.getDeptUsers(deptUsersVo);
	}

	@PostMapping("/getRoleUsers/{roleId}")
	@ApiOperation(value = "根据角色获取用户列表")
	public List<UserVO> getRoleUsers(@PathVariable Long roleId){
		return userService.getRoleUsers(roleId);
	}

	@PostMapping("/getSelectData")
	@ApiOperation(value = "获取到选人组件的数据")
	public Object getSelectData(@Valid@RequestBody SelectDataVo selectDataVo){
		return userService.getSelectData(selectDataVo);
	}

	@PostMapping("/filterTreeData")
	@ApiOperation(value = "选人组件的过滤查询")
	public Object filterTreeData(@RequestBody Map<String, Object> params) {
		return userService.filterTreeData(params);
	}

}
